﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Santa_sAdventure.Controller.Buttons
{
    internal class MenuButton
    {
        // Variables
        private Texture2D m_texture;
        private Vector2 m_position;
        private Vector2 m_originalPosition;
        private Color m_color;
        private bool m_buttonDown = false;
        private Vector2 m_size;
        private Rectangle m_btnRectangle;
        // What scale the btn should be compared to screenSize
        private int m_scaleX = 2;
        private int m_scaleY = 4;
        // How fast will the btn color drop
        private byte m_colorRate = 3;

        // Construct
        public MenuButton(Texture2D a_texture, Vector2 a_position, int a_screenWidth, int a_screenHeight)
        {
            // Texture
            m_texture = a_texture;
            // Position of the btn
            m_position = a_position;
            // Color of the btn
            m_color = new Color(255, 255, 255, 255);
            // Example: ScreenW = 800 = btnW = 100, ScreenH = 600 = btnH = 20
            m_size = new Vector2(a_screenWidth / m_scaleX, a_screenHeight / m_scaleY);
            // Update position
            m_position.X -= m_size.X / 2;
            m_position.Y -= m_size.Y / 2;
            m_originalPosition = m_position;
            // Btn rectangle
            m_btnRectangle = new Rectangle((int)m_position.X, (int)m_position.Y, (int)m_size.X, (int)m_size.Y);
        }

        // Position
        internal Vector2 Position { get { return m_position; } set { m_position = value; } }

        // Is btn clicked
        public bool IsClicked;

        // Update
        public void Update(bool a_isPressed, int a_mouseXPos, int a_mouseYPos)
        {
            // Mouse Rectangle, size = 1 to make it accurate
            Rectangle mouseRec = new Rectangle(a_mouseXPos, a_mouseYPos, 1, 1);

            // If mouseRec is inside our btn
            if (mouseRec.Intersects(m_btnRectangle))
            {
                // If color has full color, btn is not pressed down
                if (m_color.A == 255)
                {
                    m_buttonDown = false;
                }
                // If color has lowest color, btn is pressed down
                if (m_color.A == 150)
	            {
		            m_buttonDown = true;
	            }
                // If btn is pressed down, increase color, else decrease color
                if (m_buttonDown)
                {
                    m_color.A += m_colorRate;
                }
                else
                {
                    m_color.A -= m_colorRate;
                }
                // If btn is pressed
                IsClicked = a_isPressed;
            }
            // Reset color if needed
            else if (m_color.A < 255)
            {
                m_color.A = 255;
                // Btn is not clicked
                IsClicked = false;
            }
        }

        // Rest position to original
        internal void ResetPosition()
        {
            m_position = m_originalPosition;
        }

        // Draw btn
        public void Draw(SpriteBatch a_spriteBatch)
        {
            // Btn rectangle
            m_btnRectangle = new Rectangle((int)m_position.X, (int)m_position.Y, (int)m_size.X, (int)m_size.Y);
            a_spriteBatch.Draw(m_texture, m_btnRectangle, m_color);
        }
    }
}
